package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Chatting;
import org.csu.secondhand.api.service.ChattingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chatting")
public class ChattingController {
    @Autowired
    ChattingService chattingService;

    @PostMapping("insertChatting")
    @ResponseBody
    public Map<Object, Object> insertChatting(String user1, String user2) {
        Map<Object, Object> map = new HashMap<>();
        Chatting chatting = chattingService.insertChatting(new Chatting(0, user1, user2, "", "", "", 0));
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", chatting);
        return map;
    }

    @GetMapping("getChattingByUser")
    @ResponseBody
    public Map<Object, Object> getChattingByUser(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Chatting> chattingList = chattingService.getChattingByUser(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", chattingList);
        return map;
    }

    @PostMapping("sendMsg")
    @ResponseBody
    public Map<Object, Object> sendMsg(String user1, String user2, String content) {
        Map<Object, Object> map = new HashMap<>();
        Chatting chatting = chattingService.sendMsg(user1, user2, content);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", chatting);
        return map;
    }

    @GetMapping("getChatting")
    @ResponseBody
    public Map<Object, Object> getChatting(String user1, String user2) {
        Map<Object, Object> map = new HashMap<>();
        Chatting chatting = chattingService.getChatting(user1, user2);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", chatting);
        return map;
    }

    @PostMapping("iReadIt")
    @ResponseBody
    public Map<Object, Object> iReadIt(long id) {
        Map<Object, Object> map = new HashMap<>();
        Chatting chatting = chattingService.iReadIt(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", chatting);
        return map;
    }
}
